//vector 扩容机制
#include <iostream>
#include <vector>
using namespace std;

void printVector(vector<int> &v)
{
    for(auto iter=v.begin();iter!=v.end();iter++)
    {
        cout<<*iter<<" ";
    }
    cout<<endl;
}

/*
*演示 vector 的容量管理和动态扩容机制
*/

int main()
{
    vector<int> v1={8,8,8,8};
    cout<<"v1:";
    printVector(v1);

    cout<<"v1 size():"<<v1.size()<<endl;//大小
    cout<<"v1 capacity():"<<v1.capacity()<<endl;//容量

    v1.push_back(7);
    cout<<"this is after insert!"<<endl;
    cout<<"v1 size():"<<v1.size()<<endl;//大小
    cout<<"v1 capacity():"<<v1.capacity()<<endl;//容量

    v1.push_back(6);
    cout<<"this is after insert!"<<endl;
    cout<<"v1 size():"<<v1.size()<<endl;//大小
    cout<<"v1 capacity():"<<v1.capacity()<<endl;//容量

    v1.push_back(5);
    cout<<"this is after insert!"<<endl;
    cout<<"v1 size():"<<v1.size()<<endl;//大小
    cout<<"v1 capacity():"<<v1.capacity()<<endl;//容量

    v1.push_back(4);
    cout<<"this is after insert!"<<endl;
    cout<<"v1 size():"<<v1.size()<<endl;//大小
    cout<<"v1 capacity():"<<v1.capacity()<<endl;//容量

    v1.push_back(3);
    cout<<"this is after insert!"<<endl;
    cout<<"v1 size():"<<v1.size()<<endl;//大小
    cout<<"v1 capacity():"<<v1.capacity()<<endl;//容量

    for(int i=0;i<3;i++)
    {
        v1.push_back(i);
    }
    cout<<"this is after insert!"<<endl;
    cout<<"v1 size():"<<v1.size()<<endl;//大小
    cout<<"v1 capacity():"<<v1.capacity()<<endl;//容量

    //调整大小
    v1.resize(18);//capacity 不变，size变为20,不够的默认用0填充
    cout<<"this is after resize!"<<endl;
    cout<<"v1 size():"<<v1.size()<<endl;//大小
    cout<<"v1 capacity():"<<v1.capacity()<<endl;//容量
    printVector(v1);

    //扩展并填充新元素
    v1.resize(20,6);
    cout<<"this is after resize!"<<endl;
    cout<<"v1 size():"<<v1.size()<<endl;//大小
    cout<<"v1 capacity():"<<v1.capacity()<<endl;//容量
    printVector(v1);

    //capacity < size
    v1.resize(5);//截断
    cout<<"this is after resize!"<<endl;
    cout<<"v1 size():"<<v1.size()<<endl;//大小
    cout<<"v1 capacity():"<<v1.capacity()<<endl;//容量
    printVector(v1);

    return 0;
}